package com.jwthhealth_pub.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jwthhealth_pub.R;

/**
 * 自定义标题栏
 * Created by 刘振 on 2017/1/22.
 */

public class TitleLayout extends RelativeLayout {

    private TitleLayoutListener titleLayoutListener;
    private final TextView titleView;
    private TextView rightTextView = null;

    public void setOnTitleLayoutListener(TitleLayoutListener titleLayoutListener) {
        this.titleLayoutListener = titleLayoutListener;
    }

    public TitleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TitleLayout);

        String title = ta.getString(R.styleable.TitleLayout_top_title);
        float titleSize = ta.getDimension(R.styleable.TitleLayout_title_size, 20);
        int titleColor = ta.getColor(R.styleable.TitleLayout_title_color, context.getResources().getColor((R.color.textColorBlack)));

        Drawable leftImg = ta.getDrawable(R.styleable.TitleLayout_left_bmg);
        float leftHeight = ta.getDimension(R.styleable.TitleLayout_left_height, 50);
        float leftWidth = ta.getDimension(R.styleable.TitleLayout_left_width, 50);

        Drawable rightImg = ta.getDrawable(R.styleable.TitleLayout_right_bmg);
        float rightHeight = ta.getDimension(R.styleable.TitleLayout_right_height, 50);
        float rightWidth = ta.getDimension(R.styleable.TitleLayout_right_width, 50);

        String leftText = ta.getString(R.styleable.TitleLayout_left_text);
        float leftTextSize = ta.getDimension(R.styleable.TitleLayout_left_textsize, 15);//默认大小真的是14？
        int leftTextColor = ta.getColor(R.styleable.TitleLayout_left_textcolor, context.getResources().getColor(R.color.titleLayoutColor));

        String rightText = ta.getString(R.styleable.TitleLayout_right_text);
        float rightTextSize = ta.getDimension(R.styleable.TitleLayout_right_textsize, 15);
        int rightTextColor = ta.getColor(R.styleable.TitleLayout_right_textcolor, context.getResources().getColor(R.color.titleLayoutColor));

        ta.recycle();

        //title
        titleView = new TextView(context);
        LayoutParams titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        titleParams.addRule(CENTER_IN_PARENT);
        titleView.setGravity(Gravity.CENTER);
        titleView.setTextSize(titleSize);
        titleView.setTextColor(titleColor);
        titleView.setText(title);
        TextPaint paint = titleView.getPaint();
        paint.setFakeBoldText(true);
        addView(titleView, titleParams);

        RelativeLayout leftLayout = new RelativeLayout(context);
        RelativeLayout rightLayout = new RelativeLayout(context);
        LayoutParams layoutParams;//包裹左右条目的布局参数

        //left
        if (leftImg != null) {
            //left img
            layoutParams = new LayoutParams((int)leftWidth*2, LayoutParams.MATCH_PARENT);
            layoutParams.leftMargin = 20;
            leftLayout.setLayoutParams(layoutParams);
//            leftLayout.setBackgroundColor(context.getColor(R.color.colorAccent));

            LayoutParams leftImgParams = new LayoutParams((int)leftWidth, (int)leftHeight);
            leftImgParams.addRule(CENTER_IN_PARENT);
            ImageView leftView = new ImageView(context);
            leftView.setBackgroundDrawable(leftImg);
            // TODO: 2017/2/21
//            leftView.setBackgroundResource(R.drawable.btn_back_bg);

            leftLayout.addView(leftView, leftImgParams);

            leftLayout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (titleLayoutListener != null) {
                        titleLayoutListener.leftclick();
                    }
                }
            });
        } else if (leftText != null){
            //left text
            layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            TextView leftTextView = new TextView(context);
            LayoutParams leftTextParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
            leftTextParams.addRule(CENTER_IN_PARENT);
            leftTextView.setLayoutParams(leftTextParams);
            leftTextView.setGravity(Gravity.CENTER);
            leftTextView.setPadding(10, 0, 0, 0);
            leftTextView.setTextSize(leftTextSize);
            leftTextView.setTextColor(leftTextColor);
            leftTextView.setText(leftText);

            leftLayout.addView(leftTextView, layoutParams);

            leftLayout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (titleLayoutListener != null) {
                        titleLayoutListener.leftTextClick();
                    }
                }
            });
        }

        addView(leftLayout);

        //right
        if (rightImg != null) {
            layoutParams = new LayoutParams((int)rightWidth*2, LayoutParams.MATCH_PARENT);

            //right img
            layoutParams.addRule(ALIGN_PARENT_RIGHT);
            rightLayout.setLayoutParams(layoutParams);

            LayoutParams rightImgParams = new LayoutParams((int)rightWidth, (int)rightHeight);
            rightImgParams.addRule(CENTER_IN_PARENT);
            final ImageView rightView = new ImageView(context);
            rightView.setBackgroundDrawable(rightImg);

            rightLayout.addView(rightView, rightImgParams);

            rightLayout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (titleLayoutListener != null) {
                        titleLayoutListener.rightclick();
                    }
                }
            });
        } else if (rightText != null){
            //right text
            layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            layoutParams.addRule(ALIGN_PARENT_RIGHT);
            rightTextView = new TextView(context);
            LayoutParams rightTextParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
            rightTextParams.rightMargin = 20;
            rightTextParams.addRule(CENTER_IN_PARENT);
            rightTextView.setLayoutParams(rightTextParams);
            rightTextView.setGravity(Gravity.CENTER);
            rightTextView.setPadding(0, 0, 15, 0);
            rightTextView.setTextSize(rightTextSize);
            rightTextView.setTextColor(rightTextColor);
            rightTextView.setText(rightText);

            rightLayout.setLayoutParams(layoutParams);
            rightLayout.addView(rightTextView, rightTextParams);
            rightLayout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (titleLayoutListener != null) {
                        titleLayoutListener.rightTextClick();
                    }
                }
            });
        }

        addView(rightLayout);

        //divide
        View divideView =  new View(context);
        layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, 1);
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
//        layoutParams.setMargins(0, 0, 0, 5);
        divideView.setLayoutParams(layoutParams);
        divideView.setBackgroundColor(getResources().getColor(R.color.dividualGray));
        addView(divideView);


//        setBackground(ContextCompat.getDrawable(context, R.drawable.bg_shadow_view));
    }

    public void setTitle(String s) {
        titleView.setText(s);
    }

    public void setRightText(String s) {
        rightTextView.setText(s);
    }

    public interface TitleLayoutListener {
        void leftclick();
        void leftTextClick();
        void rightclick();
        void rightTextClick();
    }
}
